<?php
/**
 * Authssl 래퍼
 *
 * 프론트엔드 암.복호화 유틸리티
 * 해당 라이브러리는 직접 호출하는것을 권장하지 않습니다.
 * 코어에서 제공하는 공개 인터페이스를 사용해주시기 바랍니다.
 *
 * @author Platform Team
 */

class utilSslwrapFrontend extends utilSslwrapAbstract
{
    /**
     * 암호화
     *
     * @param string    $sHost (호스트 - 예 : http://a.com)
     * @param mixed     $mData (대상 데이터 - 예 : array('a' => 'abcd'))
     * @return utilSslwrapException | string
     */
    public static function encrypt($sHost, $mData)
    {
        $sUrl = self::_getCryptUrl($sHost);

        $aParam = array(
            'auth_mode' => 'encrypt',
            'auth_string' => self::_convEncrypt($mData)
        );

        $mResponse = self::_curl($sUrl, $aParam);
        $mResult = self::_result($mResponse);

        return $mResult;
    }

    /**
     * 복호화
     *
     * @param string    $sHost (호스트 - 예 : http://a.com)
     * @param string    $sData (암호화 문자열)
     * @param string    $sHash (해시키 - 예 : 623d8db49223f8ecef34ae8144115b86)
     * @return utilSslwrapException | string
     */
    public static function decrypt($sHost, $sData, $sHash)
    {
        $sUrl = self::_getCryptUrl($sHost);

        $aParam = array(
            'auth_mode' => 'decrypt',
            'auth_string' => $sData,
            'hKey' => $sHash
        );

        $mResponse = self::_curl($sUrl, $aParam);
        $mResult = self::_result($mResponse);

        return is_object($mResult) ? $mResult : self::_convDecrypt($mResult);
    }

    /**
     * HASH 생성
     *
     * @param string $sHost
     * @param string $sPasswd
     * @param string $sPath
     * @return utilSslwrapException | true
     */
    public static function createHash($sHost, $sPasswd, $sPath)
    {
        $sUrl = self::_getHashManagerUrl($sHost);

        return self::_createHash($sUrl, $sPasswd, $sPath);
    }

    /**
     * HASH 갱신
     *
     * @param string $sHost
     * @param string $sPasswd
     * @param string $sPath
     * @return utilSslwrapException | true
     */
    public static function refreshHash($sHost, $sPasswd, $sPath)
    {
        $sUrl = self::_getHashManagerUrl($sHost);

        return self::_refreshHash($sUrl, $sPasswd, $sPath);
    }

    /**
     * HASH 만료
     *
     * @param string $sHost
     * @param string $sPasswd
     * @param string $sPath
     * @return utilSslwrapException | true
     */
    public static function expireHash($sHost, $sPasswd, $sPath)
    {
        $sUrl = self::_getHashManagerUrl($sHost);

        return self::_expireHash($sUrl, $sPasswd, $sPath);
    }

    /**
     * IDC 별 유효 호스트 반환
     * 기본은 KT
     *
     * !!NOTICE!!
     * 1 : KT
     * 2 : SK
     *
     * @param string $sPath
     * @return string
     */
    public static function getHostByIDC($sPath=null)
    {
        $sTemplate = '%s.cafe24ssl.com';
        if (is_file($sPath) === false) {
            return sprintf($sTemplate, 'login2');
        }

        $sSource = file_get_contents($sPath);
        $sSource = trim($sSource);

        if ($sSource == '2') {
            return sprintf($sTemplate, 'login2-sk');
        }

        return sprintf($sTemplate, 'login2');
    }

    private static function _getCryptUrl($sHost)
    {
        return $sHost . '/crypt/AuthSSLManagerV2.php';
    }

    private static function _getHashManagerUrl($sHost)
    {
        return $sHost . '/crypt/AuthManageKey.php';
    }
}
